package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"github.com/duke-git/lancet/v2/fileutil"
	"github.com/ncruces/zenity"
	"video2m3u8/filters"
	"video2m3u8/layouts"
	"video2m3u8/logging"
	"video2m3u8/resources"
	"video2m3u8/setting"
	"video2m3u8/setting/oss"
	"video2m3u8/utils"
)

var ffmpegPathDef = utils.GetFFmpegPath()

// 全局变量
var ffmpegInput = widget.NewEntry()
var videoInput = widget.NewEntry()
var m3u8Input = widget.NewEntry()

func main() {

	w := layouts.App.NewWindow("视频转换")
	w.SetMainMenu(makeMenu(w))
	w.SetIcon(resources.Icon)
	layouts.TopWindow = w
	w.SetContent(makeLayout(w))
	w.SetMaster()
	w.Resize(fyne.NewSize(960, 640))
	w.ShowAndRun()
}

func makeMenu(w fyne.Window) *fyne.MainMenu {
	a := layouts.App
	themes := fyne.NewMenuItem("主题", nil)
	themes.ChildMenu = fyne.NewMenu("主题",
		fyne.NewMenuItem("暗黑", func() {
			a.Settings().SetTheme(setting.Dark())
		}),
		fyne.NewMenuItem("亮色", func() {
			a.Settings().SetTheme(setting.Light())
		}),
	)
	setOss := fyne.NewMenuItem("OSS配置", func() {
		oss.SettingOss(a, w)
	})
	quit := fyne.NewMenuItem("退出", func() {
		a.Quit()
	})
	quit.IsQuit = true
	file := fyne.NewMenu("设置",
		setOss,
		themes,
		quit,
	)
	return fyne.NewMainMenu(file)
}

// makeLayout
func makeLayout(w fyne.Window) fyne.CanvasObject {
	buildSelect := makeSelectTop()
	action := makeAction()
	logLayout := makeLogLayout()
	vbox := container.NewVBox(
		buildSelect,
		widget.NewSeparator(),
		action,
	)
	con := container.NewBorder(vbox, nil, nil, nil, logLayout)
	return con
}

func makeLogLayout() fyne.CanvasObject {
	// 创建一个滚动容器
	logging.LogDataInput.SetText("日志信息")
	logging.LogDataInput.Wrapping = fyne.TextWrapWord
	clearLogButton := container.NewVBox(
		layout.NewSpacer(),
		widget.NewButton("清空", func() {
			logging.LogDataInput.SetText("")
		}),
		layout.NewSpacer(),
	)
	content := container.NewBorder(widget.NewSeparator(), nil, nil, clearLogButton,
		container.New(layout.NewStackLayout(), logging.LogScrollContainer),
	)
	return content
}

func makeAction() fyne.CanvasObject {
	//start
	startButton := widget.NewButton("开始", startButtonClick())
	//重置数据
	resetButton := widget.NewButton("重置", func() {
		ffmpegInput.SetText(ffmpegPathDef)
		videoInput.SetText("")
		m3u8Input.SetText("")
	})
	return container.NewHBox(
		layout.NewSpacer(),
		startButton,
		layout.NewSpacer(),
		resetButton,
	)
}

func startButtonClick() func() {
	return func() {
		if ffmpegInput.Text == "" {
			zenity.Info("请选择FFMPEG路径", zenity.Title("提示"))
			return
		}
		if videoInput.Text == "" {
			zenity.Info("请选择视频文件", zenity.Title("提示"))
			return
		}
		if m3u8Input.Text == "" {
			zenity.Info("请选择输出目录", zenity.Title("提示"), zenity.WarningIcon)
			return
		}
		if !fileutil.IsExist(videoInput.Text) {
			zenity.Error("视频文件不存在", zenity.Title("提示"))
			return
		}
		utils.StartVideo2M3u8(ffmpegInput.Text, videoInput.Text, m3u8Input.Text, logging.LogChan)
	}
}

func makeSelectTop() fyne.CanvasObject {
	ffmpegTitle := widget.NewLabel("FFMPEG路径")
	ffmpegTitle.TextStyle = fyne.TextStyle{Bold: true}
	ffmpegInput.SetText(ffmpegPathDef)
	ffmpegButton := widget.NewButton("选择", func() {
		filePath, err := zenity.SelectFile(
			zenity.FileFilter{Name: "ffmpeg可执行文件", Patterns: filters.FfmpegSuffix})
		if err != nil {
			zenity.Warning("取消了选择", zenity.Title("错误"))
			return
		}
		ffmpegInput.SetText(filePath)
	})
	ffmpegLayout := container.NewBorder(nil,
		nil, ffmpegTitle, ffmpegButton, ffmpegInput)
	//视频路径
	videoTitle := widget.NewLabel("视频路径")
	videoTitle.TextStyle = fyne.TextStyle{Bold: true}
	videoInput.SetPlaceHolder("请选择视频文件")
	videoButton := widget.NewButton("选择", func() {
		filePath, err := zenity.SelectFile(
			zenity.FileFilter{Name: "视频文件", Patterns: filters.VideoSuffix})
		if err != nil {
			zenity.Warning("取消了选择", zenity.Title("错误"))
			return
		}
		videoInput.SetText(filePath)
	})
	videoLayout := container.NewBorder(nil,
		nil, videoTitle, videoButton, videoInput)
	//输出目录
	m3u8Title := widget.NewLabel("输出目录")
	m3u8Title.TextStyle = fyne.TextStyle{Bold: true}
	m3u8Input.SetPlaceHolder("请选择M3U8输出目录")
	m3u8Button := widget.NewButton("选择", func() {
		filePath, err := zenity.SelectFile(
			zenity.Directory())
		if err != nil {
			zenity.Warning("取消了选择", zenity.Title("错误"))
			return
		}
		m3u8Input.SetText(filePath)
	})
	m3u8Layout := container.NewBorder(nil,
		nil, m3u8Title, m3u8Button, m3u8Input)
	content := container.New(layout.NewGridLayoutWithColumns(1),
		ffmpegLayout,
		videoLayout,
		m3u8Layout,
	)
	return content
}
